/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.braveo.dict.javadoc;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.logging.Logger;

/**
 *
 * @author jeff.huang
 */
public class ConfigManager {
    private final static ConfigManager manager = new ConfigManager();

    private static final String PROPERTY_FILE_NAME = "Javadoc2DictConfig.xml";
    private static final String WORD_DIR_PROPERTY = "com.braveo.dict.javadoc.WorkDir";
    private static final String STORAGE_DIR_PROPERTY = "com.braveo.dict.javadoc.StorageDir";

    private ConfigManager() {

    }

    private Properties loadConfig(){
        Properties p = new Properties();

        FileInputStream fis = null;
        try{
            fis = new FileInputStream(PROPERTY_FILE_NAME);
            p.loadFromXML(fis);
            return p;
        }catch(IOException ioe){
            Logger.getLogger(this.getClass().getName()).warning(PROPERTY_FILE_NAME + " : " + ioe.getMessage());
            return p;
        }finally{
            if(fis!=null){
                try{
                    fis.close();
                }catch(IOException e){
                    //do nothing
                }
            }
        }
    }

    private void storeConfig(Properties p){
        FileOutputStream fos = null;

        try{
            fos = new FileOutputStream(PROPERTY_FILE_NAME);
            p.storeToXML(fos, "Javadoc2Doc Configuration File");
        }catch(IOException ioe){
            Logger.getLogger(this.getClass().getName()).warning(PROPERTY_FILE_NAME + " : " + ioe.getMessage());
        }finally{
            if(fos!=null){
                try{
                    fos.close();
                }catch(IOException ioe){
                    //do nothing
                }
            }
        }
    }

    protected String getConfigItem(String configId){
        Properties p = loadConfig();
        return p.getProperty(configId);
    }

    protected void setConfigItem(String configId, Object value){
        Properties p = loadConfig();
        p.setProperty(configId, value.toString());
        storeConfig(p);
    }

    public static ConfigManager getManager() {
        return manager;
    }

    public String getWordDir(){
        return getConfigItem(WORD_DIR_PROPERTY);
    }

    public void setWorkDir(String workDir){
        setConfigItem(WORD_DIR_PROPERTY, workDir);
    }

    public String getStorageDir(){
        return getConfigItem(STORAGE_DIR_PROPERTY);
    }

    public void setStorageDir(String dir){
        setConfigItem(STORAGE_DIR_PROPERTY, dir);
    }





}
